package com.yungee.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * FileUtil
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Slf4j
public class FileUtil {

    /**
     * 获取文件倒数行内容
     */
    public static List<String> readLastNLine(File file, long lastNum) {
        List<String> result = new ArrayList<String>();
        long count = 0;

        if (!file.exists() || file.isDirectory() || !file.canRead()) {
            return null;
        }

        try (RandomAccessFile fileRead = new RandomAccessFile(file, "r")) {
            long length = fileRead.length();
            if (length == 0L) {
                return result;
            } else {
                long pos = length - 1;
                while (pos > 0) {
                    pos--;
                    fileRead.seek(pos);
                    if (fileRead.readByte() == '\n') {
                        String line = new String(fileRead.readLine().getBytes("ISO-8859-1"), "utf-8");
                        result.add(line);
                        count++;
                        if (count == lastNum) {
                            break;
                        }
                    }
                }
                if (pos == 0) {
                    fileRead.seek(0);
                    result.add(fileRead.readLine());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Collections.reverse(result);
        return result;
    }

    /**
     * 查询文件内容
     */
    public static List<String> searchFileContents(String fileName, String content) {
        List<String> list = new ArrayList<String>();
        if (content == null || content.length() <= 0) {
            return list;
        }
        try (BufferedReader bufferedReader =
                new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "utf-8"))) {
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                if (StringUtils.isNotBlank(lineTxt) && lineTxt.contains(content)) {
                    list.add(lineTxt);
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return list;
    }

    public static String getFileExtension(MultipartFile file) {
        if (file == null) {
            return null;
        }
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            return null;
        }
        int dotIndex = fileName.lastIndexOf('.');
        return (dotIndex == -1) ? null : fileName.substring(dotIndex + 1);
    }

    public static String md5Hex(MultipartFile file){
        // 获取上传文件的MD5值
        String md5 = null;
        try {
            md5 = DigestUtils.md5Hex(file.getBytes());
        } catch (IOException e) {
            log.error("获取上传文件的MD5值失败", e);
        }
        return md5;
    }
    public static void mkdir(String filePath){
        // 获取上传文件的MD5值
        File tempDir = new File(filePath); // 替换为实际要设置的临时目录路径
        if (!tempDir.exists()) {
            tempDir.mkdirs();
        }
    }
}
